package main.java.indi.zyj.coderecord;

public class CoinChange {

    public int coinChange(int[] coins, int amount) {

        // 表示最少有dp[i]个硬币满足amount
        int[] dp = new int[amount + 1];
        //初始化dp数组为最大值
        for (int j = 1; j < dp.length; j++) {
            dp[j] = Integer.MAX_VALUE;
        }

        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                //只有dp[j-coins[i]]不是初始最大值时，该位才有选择的必要
                if (dp[j - coins[i]] != Integer.MAX_VALUE) {
                    dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
                }
            }
        }

        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];

    }

}
